// 厦门大学计算机专业 | 前华为工程师
// 专注《零基础学编程系列》  http://lblbc.cn/blog
// 包含：Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
// 公众号：蓝不蓝编程

import SwiftUI
import SDWebImageSwiftUI

struct OrderListView: View {
    @StateObject private var viewModel = OrderViewModel()
    
    var body: some View {
        VStack{
            OrderPageView(viewModel: viewModel)
                .padding(EdgeInsets.init(top: 0, leading: 0, bottom: 1, trailing: 0))
        }
    }
}

struct LblTab{
    var id: Int = 0
    var name: String = ""
    
    init(id:Int, name:String)
    {
        self.id = id
        self.name = name
    }
}

struct OrderPageView: View {
    @StateObject var viewModel: OrderViewModel
    
    var body: some View {
        List {
            ForEach(viewModel.dataList.indices , id: \.self){ i in
                let orderInfo = viewModel.dataList[i]
                
                NavigationLink(destination: OrderDetailView(orderId: orderInfo.orderId)){
                    buildOrderItemView(orderInfo: orderInfo)
                }
            }
        }.onAppear(perform: {
            viewModel.queryOrderByStatusResp(orderStatus: 0)
        })
    }
    
    private func buildOrderItemView(orderInfo: OrderInfo) -> some View{
        return VStack{
            buildOrderListView(list: orderInfo.list)
            
            HStack{
                Spacer()
                Text("共"+String(orderInfo.list.count)+"件商品").padding()
            }
            
            HStack{
                Spacer()
                Text("删除订单").padding(EdgeInsets.init(top: 5, leading: 10, bottom: 5, trailing: 10))
                    .background(RoundedRectangle(cornerRadius: 50).strokeBorder(Color.gray,lineWidth: 1))
                    .foregroundColor(Color(hex: 0x141414))
            }
        }
    }
    private func buildOrderListView(list: [OrderDetail]) -> some View{
        return VStack{
            ForEach(list.indices , id: \.self){ j in
                let orderDetail = list[j]
                HStack{
                    WebImage(url: URL(string: orderDetail.squarePic ?? ""))
                        .placeholder{Color.gray}
                        .resizable()
                        .scaledToFit()
                        .frame(width: 50, height: 50)
                    VStack(alignment: .leading){
                        Text(orderDetail.name).padding(EdgeInsets.init(top: 0, leading: 0, bottom: 5, trailing: 0))
                        HStack{
                            Text("￥" + String(orderDetail.price/100))
                            Spacer()
                            Text("×" + String(orderDetail.quantity))
                        }
                    }
                }
            }
        }
    }
}


//struct OrderListView_Previews: PreviewProvider {
//    static var previews: some View {
//        OrderListView()
//    }
//}
